/*This file is part of Sandbox.

Sandbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.

Sandbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Sandbox.  If not, see <http://www.gnu.org/licenses/>.

For contact, see the About page of my blog: <http://mygeekyware.wordpress.com/>.*/

#include <iostream>
#include "dibujar.h"
#include "particula.h"
#include <SDL/SDL.h>
#include <SDL/SDL_events.h>
#include <SDL/SDL_ttf.h>
#include "controlador.h"
#include "definiciones.h"

using namespace std;


SDL_Rect separador = (SDL_Rect) {0,440,800,1};
SDL_Rect selected = (SDL_Rect) {0,0,4,9}; //Dot to show which option is active
SDL_Rect selected2 = (SDL_Rect) {0,0,3,4};
SDL_Rect part = (SDL_Rect) {0,0,2,2};


void dibujar(particula matriz[400][220], int tipo, TTF_Font *font, SDL_Surface **screen, controlador *contr) {
    SDL_FillRect(*screen, NULL, (Uint32)0); //Clear back buffer to black
    SDL_FillRect(*screen, &separador, (Uint32)SDL_MapRGB((*screen)->format,255,255,255)); //Separador de menu.
    dibujarmenu(font, screen, contr);
    int mx,my;
    if (SDL_GetMouseState(&mx, &my) & SDL_BUTTON(1)) {
        cout << mx << " " << my << endl;
    }
    for (mx = 0; mx < 400; mx++) {
        for (my = 0; my < 220; my++) {
            if (matriz[mx][my].tipo != 0) {
            part.x = mx*2; part.y = my*2;
            if (matriz[mx][my].tipo == FIREWORK && matriz[mx][my].contador < 5) {
                SDL_FillRect(*screen, &part, give_color(-1,screen));
            }
            else if (matriz[mx][my].tipo == FUSE && matriz[mx][my].contador > 0) {
                SDL_FillRect(*screen, &part, give_color(FIREWORK,screen)); //FIREWORK because it's yellow, nothing special, I don't have to make a new color this way
            }
            else
                SDL_FillRect(*screen, &part, give_color(matriz[mx][my].tipo,screen));
            }
            /*if (matriz[mx][my].temperatura < 0) {
                part.x = mx*2; part.y = my*2;
                SDL_FillRect(*screen, &part, give_color(EARTH,screen));
            }*/
        }
    }
    SDL_Flip(*screen); //Swap buffers
}



void dibujarseleccionado(SDL_Surface **screen, controlador *contr) { //To draw the red square in selected option
    Uint32 color_s = SDL_MapRGB((*screen)->format,255,0,0);
    Uint32 color_s2 = SDL_MapRGB((*screen)->format,0,0,255);
    switch (contr->brtype) {
        case CLEAR:
            selected.x = 0; selected.y = 442;
            break;
        case WALL:
            selected.x = 0; selected.y = 455;
            break;
        case WATER:
            selected.x = 0; selected.y = 468;
            break;
        case SALT:
            selected.x = 0; selected.y = 481;
            break;
        case SAND:
            selected.x = 0; selected.y = 494;
            break;
        case FIRE:
            selected.x = 0; selected.y = 507;
            break;
        case SALTWATER:
            selected.x = 0; selected.y = 520;
            break;
        case STONE:
            selected.x = 0; selected.y = 533;
            break;
        case EARTH:
            selected.x = 0; selected.y = 546;
            break;
        case ICE:
            selected.x = 0; selected.y = 559;
            break;
        case MAGMA:
            selected.x = 0; selected.y = 572;
            break;
        case FIREWORK:
            selected.x = 0; selected.y = 585;
            break;
        case TORCH:
            selected.x = 88; selected.y = 442;
            break;
        case BOMB:
            selected.x = 88; selected.y = 455;
            break;
        case FUSE:
            selected.x = 88; selected.y = 469;
            break;
        case OIL:
            selected.x = 88; selected.y = 482;
            break;
    }
    switch (contr->brtype2) {
        case CLEAR:
            selected2.x = 0; selected2.y = 447;
            break;
        case WALL:
            selected2.x = 0; selected2.y = 460;
            break;
        case WATER:
            selected2.x = 0; selected2.y = 473;
            break;
        case SALT:
            selected2.x = 0; selected2.y = 486;
            break;
        case SAND:
            selected2.x = 0; selected2.y = 499;
            break;
        case FIRE:
            selected2.x = 0; selected2.y = 512;
            break;
        case SALTWATER:
            selected2.x = 0; selected2.y = 525;
            break;
        case STONE:
            selected2.x = 0; selected2.y = 538;
            break;
        case EARTH:
            selected2.x = 0; selected2.y = 551;
            break;
        case ICE:
            selected2.x = 0; selected2.y = 564;
            break;
        case MAGMA:
            selected2.x = 0; selected2.y = 577;
            break;
        case FIREWORK:
            selected2.x = 0; selected2.y = 590;
            break;
        case TORCH:
            selected2.x = 88; selected2.y = 447;
            break;
        case BOMB:
            selected2.x = 88; selected2.y = 460;
            break;
        case FUSE:
            selected2.x = 88; selected2.y = 473;
            break;
        case OIL:
            selected2.x = 88; selected2.y = 486;
            break;
    }
    SDL_FillRect(*screen, &selected, color_s);
    SDL_FillRect(*screen, &selected2, color_s2);
}

Uint32 give_color(int tipo, SDL_Surface **screen) {
    switch (tipo) {
        case CLEAR:
            return SDL_MapRGB((*screen)->format,0,0,0);
        case WALL:
            return SDL_MapRGB((*screen)->format,150,150,150);
        case WATER:
            return SDL_MapRGB((*screen)->format,30,30,255);
        case SALT:
            return SDL_MapRGB((*screen)->format,255,255,255);
        case SAND:
            return SDL_MapRGB((*screen)->format,255,204,75);
        case FIRE:
            return SDL_MapRGB((*screen)->format,255,0,0);
        case SALTWATER:
            return SDL_MapRGB((*screen)->format,70,255,255);
        case STONE:
            return SDL_MapRGB((*screen)->format,180,180,180);
        case EARTH:
            return SDL_MapRGB((*screen)->format,139,41,0);
        case PLANT:
            return SDL_MapRGB((*screen)->format,0,255,0);
        case STEAM:
            return SDL_MapRGB((*screen)->format,200,200,255);
        case ICE:
            return SDL_MapRGB((*screen)->format, 255,255,255);
        case MAGMA:
            return SDL_MapRGB((*screen)->format, 210,0,0);
        case FIREWORK:
            return SDL_MapRGB((*screen)->format, 255,255,0);
        case TORCH:
            return SDL_MapRGB((*screen)->format, 139,41,0);
        case BOMB:
            return SDL_MapRGB((*screen)->format, 52,89,0);
        case FUSE:
            return SDL_MapRGB((*screen)->format, 149,66,0);
        case OIL:
            return SDL_MapRGB((*screen)->format, 145,43,0);
    }
}